#ifndef DATA_POOL_H
#define DATA_POOL_H

#include "common.h"
#include "database/db_manager.h"  // 新增数据库依赖
#include "event_loop.h"

typedef struct {
    data_item_t *hash_table;
    pthread_rwlock_t rwlock;
    size_t max_size;
    size_t current_size;
    event_loop_t *event_loop;
    db_manager_t *db_mgr;         // 新增数据库管理器
    bool persist_to_db;           // 是否持久化到数据库
} data_pool_t;

int data_pool_init(data_pool_t *pool, size_t max_size, event_loop_t *event_loop, db_manager_t *db_mgr);
int data_pool_put(data_pool_t *pool, const char *key, json_t *value);
int data_pool_put_with_timestamp(data_pool_t *pool, const char *key, json_t *value, time_t timestamp);
json_t *data_pool_get(data_pool_t *pool, const char *key);
int data_pool_get_with_timestamp(data_pool_t *pool, const char *key, json_t **value, time_t *timestamp);
int data_pool_remove(data_pool_t *pool, const char *key);
void data_pool_clear(data_pool_t *pool);
void data_pool_destroy(data_pool_t *pool);
size_t data_pool_size(data_pool_t *pool);

// 新增数据库相关函数
int data_pool_load_from_db(data_pool_t *pool);
int data_pool_sync_to_db(data_pool_t *pool);
int data_pool_set_persistence(data_pool_t *pool, bool persist);

data_item_t **data_pool_find_by_pattern(data_pool_t *pool, const char *pattern, size_t *count);
void data_pool_free_search_result(data_item_t **result);

#endif
